<?php

namespace app\admin\controller;

use app\admin\model\PohtoInstitution as PohtoInstitutionModel;
use app\admin\service\OssService;
use app\admin\validate\CommonnValidate;
use app\admin\validate\PhotoValidate;
use app\BaseController;

class PhotoInstitution extends BaseController
{
    protected $middleware = ['Auth'];
    public function allList()
    {
        $params = $this->request->post(['name']);
        $sql = new PohtoInstitutionModel();
        $whereArr = [];
        if(isset($params['name']) && $params['name']) {
            $whereArr[] = ["name", "LIKE", '%'.$params['name'].'%'];
        }
        $list = $sql->where($whereArr)->order('sort desc')
            ->order('create_at desc')->select();
        if(config('app.imgtype') == 'other') {
            foreach ($list as $k => $item) {
                $item['image_show'] = $item['image_path'];
            }
            return result()->success($list);
        } else {
            $oss = new OssService();
            return result()->success($oss->signIds($list, 'cover_id', 'cover_show'));
        }
    }
    public function list() {
        $params = $this->request->post(['current', 'size', 'name']);
        $validate = new CommonnValidate();
        if (!$validate->scene('list')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new PohtoInstitutionModel();
        $whereArr = [];
        if(isset($params['name']) && $params['name']) {
            $whereArr[] = ["name", "LIKE", '%'.$params['name'].'%'];
        }
        $list = $sql
            ->where($whereArr)
            ->order('sort desc')
            ->order('create_at desc')
            ->paginate([
                'list_rows'=> request()->param('size'),
                'page' => request()->param('current'),
            ])->toArray();
        if(config('app.imgtype') == 'other') {
            foreach ($list['data'] as $k => $item) {
                $list['data'][$k]['cover_show'] = $item['cover'];
            }
        } else {
            $oss = new OssService();
            $list['data'] = $oss->signIds($list['data'], 'cover_id', 'cover_show');
        }
        return result()->success($list);
    }

    public function addEdit() {
        $params = $this->request->post(['id']);
        if(isset($params['id']) && $params['id']) {
            return $this->edit();
        }
        return $this->add();
    }

    public function add() {
        $params = $this->request->post(['name', 'introduce', 'cover', 'cover_id', 'sort']);
        $validate = new PhotoValidate();
        if (!$validate->scene('addinstitution')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new PohtoInstitutionModel();
        $sql->save($params);
        return result()->success("添加成功");
    }

    public function edit() {
        $params = $this->request->post(['id', 'name', 'introduce', 'cover', 'cover_id', 'sort']);
        $validate = new CommonnValidate();
        if (!$validate->scene('edit')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new PohtoInstitutionModel();
        $info = $sql->find($params['id']);
        $info->save($params);
        return result()->success("修改成功");
    }

    public function dels() {
        $params = $this->request->post(['ids']);
        $validate = new CommonnValidate();
        if (!$validate->scene('dels')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new PohtoInstitutionModel();
        $sql->destroy($params['ids']);
        return result()->success("删除成功");
    }
}